<template>
  <div>
    <el-form ref="deptForm" :rules="rules" label-width="120px" :model="form">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in OriginList" :key="item.id" value="1" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// 获取下拉员工列表
import { getEmployess } from '@/api/employees'
// 获取增加部门数据    修改
import { addDepartMent, updateDepartMent, getDepartMentId } from '@/api/departments'
export default {
  props: {
    id: {
      type: String,
      required: true
    },
    IsEdit: {
      type: Boolean,
      required: true
    },
    mapList: {
      type: Array,
      required: true
    }
  },
  data() {
    // 需求 部门编码要不一样 在所有的数据里
    const validRules = (rules, value, callback) => {
      // 我得要拿到所有的数据 进行筛选
      console.log(this.mapList.map(item => item.code))
      this.mapList.map(item => item.code).includes(value) ? callback(new Error('编码重复了')) : callback()
    }
    return {
      // 1 数据交互 这个是用来进行交互的 修改的时候 会拿来进行赋值显示
      form: {
        name: '', // 名字
        code: '', // 编码
        manager: '', // 负责人
        introduce: '' // 介绍
      },
      OriginList: [], // 获取的所有员工
      rules: {
        name: [{ required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        code: [{ required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 3 到 5 个字符', trigger: 'blur' },
          { validator: validRules, trigger: 'blur' }
        ],
        manager: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        introduce: [{ required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 3 到 5 个字符', trigger: 'blur' }

        ]
      }
    }
  },
  created() {
    // 获取员工数据
    this.loadEmployee()
    if (this.IsEdit) {
      this.loadGetDepartMentId()
    }
  },
  methods: {
    // 根据id获取信息
    async loadGetDepartMentId() {
      try {
        const res = await getDepartMentId(this.id)
        console.log(res)
        this.form = res.data
      } catch (error) {
        console.log(error)
      }
    },
    // 修改数据
    async loadUpdateDepartMent() {
      const res = await updateDepartMent(this.form.id)
      console.log(res, '修改数据')
    },
    // 获取增加数据
    async loadAddDepartMent() {
      const res = await addDepartMent({ ...this.form, pid: this.id })
      console.log(res)
      this.$emit('successSon')
    },
    // 获取下拉员工列表
    async  loadEmployee() {
      const res = await getEmployess()
      console.log(res, '获取员工数据')
      this.OriginList = res.data
    },
    //  2 确认按钮
    hSubmit() {
      // 触发增加数据
      this.$refs.deptForm.validate(valid => {
        if (!valid) return this.$message.error('兜底结果不达标')
        this.IsEdit ? this.loadUpdateDepartMent() : this.loadAddDepartMent()
      })
    },
    // 3 取消按钮
    hCancel() {

    }
  }
}
</script>

<style>

</style>
